<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>测试</title>
    <link href="https://unpkg.com/layui@2.7.6/dist/css/layui.css" rel="stylesheet">

    <script src="https://unpkg.com/layui@2.7.6/dist/layui.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <script src="https://cdn.bootcss.com/spark-md5/3.0.0/spark-md5.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/aes.js"></script>
</head>

<body>

<form class="layui-form" id="formData">
    <div class="layui-form-item">
        <label class="layui-form-label">姓名</label>
        <div class="layui-input-block">
            <input type="text" name="realName" required lay-verify="required" placeholder="请输入姓名" value="王测试" autocomplete="off"
                   class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">年龄</label>
        <div class="layui-input-block">
            <input type="text" name="age" value="25" placeholder="请输入手机号" autocomplete="off"
                   class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">性别</label>
        <div class="layui-input-block">
            <input type="radio" name="sex" value="1" title="男" checked>
            <input type="radio" name="sex" value="2" title="女">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">手机号</label>
        <div class="layui-input-block">
            <input type="text" name="mobile" required lay-verify="phone" value="17698987878" placeholder="请输入手机号" autocomplete="off"
                   class="layui-input">
        </div>
    </div>



    <div class="layui-form-item">
        <label class="layui-form-label">身份证</label>
        <div class="layui-input-block">
            <input type="text" name="idCard" required lay-verify="identity" value="110101199001018770" placeholder="请输入身份证" autocomplete="off"
                   class="layui-input">
        </div>
    </div>




    <div class="layui-form-item">
        <label class="layui-form-label">省份</label>
        <div class="layui-input-block">
            <select name="province" id="province" lay-filter="provinceSelect" lay-verify="required">
                <option value="">请选择省份</option>
                <option value="北京市">北京市</option>
                <option value="上海市">上海市</option>
                <option value="浙江省">浙江省</option>
            </select>
        </div>
    </div>

    <input type="hidden" name="provinceCode" id="hiddenProvinceCode">
    <input type="hidden" name="cityCode" id="hiddenCityCode">

    <input type="hidden" name="userIp"  autocomplete="off" class="layui-input" value="0.0.0.0" readonly>

    <div class="layui-form-item">
        <label class="layui-form-label">城市</label>
        <div class="layui-input-block">
            <select name="city" id="city" lay-filter="citySelect" lay-verify="required">
                <option value="">请选择城市</option>
            </select>
        </div>
    </div>


    <div class="layui-form-item">
        <label class="layui-form-label">贷款金额</label>
        <div class="layui-input-block">
            <select name="loanAmount" lay-verify="required">
                <option value="la005">20万以上</option>
                <option value="la004">10-20万</option>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">贷款时间</label>
        <div class="layui-input-block">
            <select name="loanTime" lay-verify="required">
                <option value="lt004">12个月以上</option>
                <option value="lt003">6-12个月</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">房产</label>
        <div class="layui-input-block">
            <select name="house" lay-verify="required">
                <option value="hu003">按揭房可抵押</option>
                <option value="hu001">无房</option>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">车产</label>
        <div class="layui-input-block">
            <select name="car" lay-verify="required">
                <option value="car002">有车可抵押</option>
                <option value="car001">无车</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">公积金</label>
        <div class="layui-input-block">
            <select name="gjj" lay-verify="required">
                <option value="gjj001">无公积金</option>
                <option value="gjj002">0-6个月</option>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">社保</label>
        <div class="layui-input-block">
            <select name="shebao" lay-verify="required">
                <option value="sb001">无社保</option>
                <option value="sb002">0-6个月</option>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">保险</label>
        <div class="layui-input-block">
            <select name="baoxian" lay-verify="required">
                <option value="bx001">无保险</option>
                <option value="bx002">0-6个月</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">芝麻分</label>
        <div class="layui-input-block">
            <select name="zhima" lay-verify="required">
                <option value="zm005">700分以上</option>
                <option value="zm004">650-699分</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">花呗</label>
        <div class="layui-input-block">
            <select name="huabei" lay-verify="required">
                <option value="hb005">20万以上</option>
                <option value="hb004">10-20万</option>
                <option value="hb001">无花呗</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">白条</label>
        <div class="layui-input-block">
            <select name="baitiao" lay-verify="required">
                <option value="bt005">20万以上</option>
                <option value="bt004">10-20万</option>
                <option value="bt001">无白条</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">收入</label>
        <div class="layui-input-block">
            <select name="shouru" lay-verify="required">
                <option value="sr004">20000元以上</option>
                <option value="sr003">10000-20000元</option>
                <option value="sr001">1-5000元</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">学历</label>
        <div class="layui-input-block">
            <select name="xueli" lay-verify="required">
                <option value="xl005">研究生及以上</option>
                <option value="xl004">本科</option>
                <option value="xl001">无学历</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">用途</label>
        <div class="layui-input-block">
            <select name="yongtu" lay-verify="required">
                <option value="yt001">消费</option>
                <option value="yt007">其他</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">有无逾期</label>
        <div class="layui-input-block">
            <select name="yuqi" lay-verify="required">
                <option value="yq001">无逾期</option>
                <option value="yq002">0-3个月</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">信用卡</label>
        <div class="layui-input-block">
            <select name="xinyongka" lay-verify="required">
                <option value="xyk001">无信用卡</option>
                <option value="xyk002">1-5万</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">微粒贷</label>
        <div class="layui-input-block">
            <select name="weili" lay-verify="required">
                <option value="wl001">无微粒贷</option>
                <option value="wl002">1-5万</option>
            </select>
        </div>
    </div>


    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn"  lay-filter="formDemo">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>

            <a class="layui-btn" target="_blank" id="pageInfo" href="">跳转授权页面</a>
        </div>
    </div>
</form>

<script>
    layui.use('form', function () {
        var form = layui.form;
        console.log(form)
        $('#pageInfo').hide();

        form.on('select(provinceSelect)', function (data) {

            var selectedProvince = data.value;
            var cities = provinceCityMap[selectedProvince];
            var citySelect = $('#city');
            citySelect.empty();
            $.each(cities, function (index, city) {
                citySelect.append('<option value="' + city + '">' + city + '</option>');
            });
            form.render('select');
        });


        // 提交
        form.on('submit', function (data) {

            let formData = $('#formData').serializeArray();

            const formDataObj = formData.reduce((a, b) => ({...a, [b.name]: b.value}), {})

            data.field = {...data.field,...formDataObj}


            //  将省份编码添加到data.field中
            var selectedProvince = $('#province').val();
            var provinceCode = provinceMap[selectedProvince];
            data.field.provinceCode = provinceCode;

            // 将城市编码添加到data.field中
            var selectedCity = $('#city').val();
            var cityCode = cityMap[selectedCity];
            data.field.cityCode = cityCode;
            data.field.mobileMd5 = SparkMD5.hash(data.field.mobile)


            const randomString = (length) => {
                const strs = '0123456789abcdefghijklmnopqrstuvwxyz'
                return Array.from({length}, () => strs[Math.floor(Math.random() * strs.length)]).join('')
            }

            const submit = async  () => {
                const result = await  $.ajax({
                    //  url: 'http://apitest.koukoudai.shop/apisource/source/checkUser',
                   url: 'http://apitest.koukoudai.shop/apisource/source/checkUser',
                    type: 'post',
                    dataType: 'json',
                    contentType: 'application/json',
                    data: JSON.stringify({
                        'channelCode': 'F590OG',
                        'tranData':data.field,
                        'requestNo': randomString(28),
                        <#--'timestamp': `${new Date().getTime()}`,-->
                    }),
                })
                console.log(result)
                if (!(result.respCode == '200' && result.data.status == '000')) return  alert('撞库失败: ' + result.respMsg);

                console.log(result.data.orderNo)
                const jinjianResult = await  $.ajax({
                    //url: 'http://apitest.koukoudai.shop/apisource/source/registerUser',
                     url: 'http://apitest.koukoudai.shop/apisource/source/registerUser',
                    type: 'post',
                    dataType: 'json',
                    contentType: 'application/json',
                    data: JSON.stringify({
                        'channelCode': 'F590OG',
                        'tranData': {...data.field, orderNo: result.data.orderNo.toString()  },
                        'requestNo': randomString(28),
                        <#--'timestamp': `${new Date().getTime()}`,-->
                    }),
                })

                console.log(jinjianResult)
                if (!(jinjianResult.respCode == '200' && jinjianResult.data.status == '000')) {
                    return alert('进件失败: ' + result.respMsg);
                }else{
                    $('#pageInfo').show();
                    $('#pageInfo').attr('href', jinjianResult.data.approveUrl);
                }
                // console.log(jinjianResult.data.approveUrl)
                // window.location.href = result.data.approveUrl
            }
            submit();

            return false;
        });
    });


    var provinceMap = {
        "北京市": "110000",
        "上海市": "310000",
        "浙江省": "330000"

    };

    var cityMap = {
        "北京市": "110100",
        "上海市": "310100",
        "杭州市": "330100",
        "宁波市": "330200",
        "温州市": "330300"
    };


    var provinceCityMap = {
        "北京市": ["北京市"],
        "上海市": ["上海市"],
        "浙江省": ["杭州市", "宁波市", "温州市"]
    };
</script>

</body>
</html>
